package com.yakumosakura.yakumoblade.event;

import com.yakumosakura.yakumoblade.registry.ItemRegistry;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.boss.wither.WitherBoss;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraftforge.event.entity.living.LivingDropsEvent;
import net.minecraftforge.event.entity.living.LivingHurtEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.registries.ForgeRegistries;

import java.util.Random;

@Mod.EventBusSubscriber
public class WitherDrop {
    @SubscribeEvent
    public static void WitherDead(LivingDropsEvent e ){
        if (e.getEntity() instanceof WitherBoss b){
            Level level = b.level();
            if(level.isClientSide){
                return;
            }

            if (level.isNight()){
                    ItemStack p_32005_ = new ItemStack(ItemRegistry.Star_Soul_Crystal.get());
                    Random r = new Random();
                    p_32005_.setCount(r.nextInt(2));
                    level.addFreshEntity(new ItemEntity(level,b.getX(),b.getY()+1,b.getZ()
                            , p_32005_));
                    if (b.getY()>=130){
                        if (level.isRaining()){
                            ItemStack p_32005_1 = new ItemStack(ItemRegistry.StarTreasurescripture.get());
                            p_32005_1.setCount(1);
                            level.addFreshEntity(new ItemEntity(level,b.getX(),b.getY()+1,b.getZ()
                                    , p_32005_1));
                        }
                    }
                }
        }
    }

}
